package com.develop.data.loan.manager.change.transfer;

import com.develop.data.loan.manager.change.transfer.api.LoanContractStateTransfer;
import com.develop.data.loan.model.LoanContract;
import com.develop.data.loan.model.LoanInstallment;
import lombok.extern.slf4j.Slf4j;
import org.javamoney.moneta.Money;

import javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
public class LoanContractCommonToCleared implements LoanContractStateTransfer {

    @Override
    public MonetaryAmount preview(LoanContract contract, LocalDateTime localDate) {

        MonetaryAmount amount = Money.zero(Monetary.getCurrency("CNY"));
        List<LoanInstallment> installments = contract.getLoanInstallments();
        for (LoanInstallment installment: installments) {
            amount = amount.add(new LoanInstallmentCommonToCleared().preview(installment, localDate));
        }
        return amount;


    }
    @Override
    public void execute(LoanContract contract, LocalDateTime localDate) {

        List<LoanInstallment> installments = contract.getLoanInstallments();
        for (LoanInstallment installment: installments) {
            new LoanInstallmentCommonToCleared().execute(installment, localDate);
        }
        contract.setCleared(true);
    }
}

